<?php
/*
* Description: FriendRequests class
*/
class FriendRequests extends DbBase{
	
	var $requestee_id;
	var $requester_id;
	
    // constructor
	public function __construct() {
        $this->table_name = 'friend_requests';
    }
	
	public function pending_requests(){
		$result = array();
		
		$sql  = "SELECT * ";
		$sql .= " FROM ".$this->table_name;
		$sql .= " WHERE requestee_id=$this->requestee_id";
		$sql .= " AND status=".(int)false;
		
		$rows = $this->query($sql);
		foreach($rows as $index => $row){
			$friendr = new FriendRequests();
			$friendr->set_attributes($row);
			$result[$index] = $friendr;
		}
		
		return $result;
	}
	
	public function submitted_requests(){
		$result = array();
		
		$sql  = "SELECT * ";
		$sql .= " FROM ".$this->table_name;
		$sql .= " WHERE requester_id=$this->requester_id";
		$sql .= " AND status=".(int)false;
		
		$rows = $this->query($sql);
		foreach($rows as $index => $row){
			$friendr = new FriendRequests();
			$friendr->set_attributes($row);
			$result[$index] = $friendr;
		}
		
		return $result;
	}
	
	public function has_requests(){
		$sql  = "SELECT count(*) as count ";
		$sql .= " FROM $this->table_name ";
		$sql .= " WHERE (requestee_id=$this->requestee_id AND requester_id=$this->requester_id) ";
		$sql .= " OR (requester_id=$this->requestee_id AND requestee_id=$this->requester_id)";
		
		$result = array_shift($this->query($sql));
		if($result['count']>0)
			return true;
		return false;
	}
	
	public function insert(){
		$sql  = "INSERT INTO $this->table_name";
		$sql .= " (requester_id,requestee_id,status)";
		$sql .= " VALUES($this->requester_id, $this->requestee_id, false)";
		return $this->execute($sql);
	}
	
	public function delete(){
		$sql  = "DELETE FROM $this->table_name";
		$sql .= " WHERE requester_id=$this->requester_id";
		$sql .= " AND requestee_id=$this->requestee_id";
		return $this->execute($sql);
	}
	
	// CLASS METHODS
	public static function get_pending_requests($user_id){
	
		$friend_request = new FriendRequests();
		$friend_request->requestee_id = $user_id;
		return $friend_request->pending_requests();
		
	}
	
	public static function has_request($user1_id, $user2_id){
	
		$request = new FriendRequests();
		$request->requester_id = $user1_id;
		$request->requestee_id = $user2_id;
		$result = $request->has_requests();
		
		if(!$result){
			$request->requester_id = $user2_id;
			$request->requestee_id = $user1_id;
			return $request->has_requests();
		} 
		else
			return true;
	}
	
	public static function delete_friend_requests($user_id){
		$request = new FriendRequests();
		$request->requester_id = $user_id;
		$request->requestee_id = $user_id;
		
		$requests = $request->submitted_requests();
		foreach($requests as $r)
			$r->delete();
		
		$requests = $request->pending_requests();
		foreach($requests as $r)
			$r->delete();
	}

	public function update(){ throw new Exception("method unimplemented yet"); }
	public function select(){ throw new Exception("method unimplemented yet"); }
}
?>